package com.cwnu.controller;


import com.cwnu.entity.Article;
import com.cwnu.entity.Result;
import com.cwnu.entity.PageBean;
import com.cwnu.entity.User;
import com.cwnu.service.ArticleService;
import com.cwnu.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    // 新增文章
    @PostMapping
    public Result add(@RequestBody @Validated Article article) {
        articleService.add(article);
        return Result.success();
    }
    // 文章分页
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId
    ) {
        PageBean<Article> pb = articleService.list(pageNum, pageSize, categoryId);
        return Result.success(pb);
    }

    // 获取当前用户的所有博客
    @GetMapping("/getWithoutPb")
    public Result getWithoutPb() {
        List<Article> articleList = articleService.getWithoutPb();
        return Result.success(articleList);
    }

    // 获取数据库中所有文章
    @GetMapping("/all")
    public Result<PageBean<Article>> listAll(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) Integer status
    ) {
        PageBean<Article> pb = articleService.listAll(pageNum, pageSize, categoryId,status);
        return Result.success(pb);
    }

    // 获取文章详情
    @GetMapping("/detail")
    public Result<Article> detail(@RequestParam Integer id) {
        Article article = articleService.findById(id);
        return Result.success(article);
    }
    // 更新文章
    @PutMapping
    public Result update(@RequestBody @Validated Article article) {
        articleService.update(article);
        return Result.success();
    }
    // 删除文章
    @DeleteMapping
    public Result delete(@RequestParam @Validated Integer id) {
        articleService.delete(id);
        return Result.success();
    }

    // 根据article_id查询用户
    @GetMapping("/getUserByArticleId")
    public Result getUser(@RequestParam("articleId") Integer articleId) {
        User user = articleService.geUser(articleId);
        return Result.success(user);
    }

    // 查询正在审核的文章
    @GetMapping("/getReviewArticle")
    public Result getReviewArticle() {
        Map<String, Object> mp = ThreadLocalUtil.get();
        Integer userId = (Integer) mp.get("id");
        List<Article> articleList = articleService.getReviewArticle(userId);
        if (articleList == null || articleList.size() == 0) {
            return Result.error("暂无文章");
        } else {
            return Result.success(articleList);
        }
    }

    // 删除正在审核中或者审核失败的文章
    @DeleteMapping("/deleteReviewArticle")
    public Result deleteReviewArticle(@RequestBody Article article) {
        // 查询当前文章的状态是否是审核中
        Integer status = article.getStatus();
        // 状态错误，返回
        if (status != 0 && status != 3) {
            return Result.error("状态错误，无法删除当前文章~");
        }
        // 处于审核中，删除
        articleService.delete(article.getId());
        return Result.success();
    }

    // 更新正在审核中或者审核失败的文章
    @PutMapping("/updateReviewArticle")
    public Result updateReviewArticle(@RequestBody Article article) {
        // 查询当前文章的状态是否是审核中
        Integer status = article.getStatus();
        // 状态错误，返回
        if (status != 0 && status != 3) {
            return Result.error("状态错误，无法更新当前文章~");
        }
        // 处于审核中，删除
        articleService.update(article);
        return Result.success();
    }

    // 根据关键字查询数据库中所有已发布的文章
    @PostMapping("/search")
    public Result search(@RequestBody Map<String, Object> params) {
        String key = (String) params.get("key");
//        key = "%" + key + "%";
        List<Article> articleList = articleService.search(key);
        if (articleList.size() == 0) return Result.error("暂未查询到文章");
        return Result.success(articleList);
    }
}
